package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.ChannelDAO;
import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.service.ChannelService;

public class ChannelServiceImpl implements ChannelService {
	
	
	private ChannelDAO chdao;
	
	
	public ChannelServiceImpl(){
		
		this.chdao=new ChannelDAO();
		
	}
	
	
	public ChannelServiceImpl(ChannelDAO chdao){
		
		this.chdao=chdao;
		
	}
	

	public boolean delPublshChannel(Channel channel) {
		
		
		//return chdao.updateChannelStatus(channel.getId(), "00");
		
		return true;
	}

	public List<Channel> getAllChannel() {
		
		return chdao.getAllChannel();
	}

	public List<Channel> getAllNoPublishChannel() {
		
		return chdao.getAllNoPublishChannel();
	}

	public List<Channel> getAllPublishChannel() {
		
		return chdao.getAllPublishChannel();
	}

	public Channel getOneChannel(Channel channel) {
		
		return (Channel) chdao.load(Channel.class, channel.getId());
	}

	public Channel getParentChannel(Channel channel) {
		
		return (Channel) chdao.load(Channel.class, channel.getParentChannelId());
	}

	public List<Channel> getPublishSubChannel(Channel channel) {
		
		return chdao.getPublishSubChannel(channel);
	}

	public List<Channel> getSubChannel(Channel channel) {
		
		return chdao.getSubChannel(channel);
	}

	public boolean publishChannel(Channel channel) {
		
		//return chdao.updateChannelStatus(channel.getId(), "01");
		
		return true;
	}

	public boolean upateChannelPri(Channel channel) {
		
		//return chdao.updateChannelPri(channel.getId(), channel.getPri());
		
		return true;
	}

	public boolean deleteChannel(Channel channel) {
		
		
		//注意删除频道时，需要检查频道下是否有子频道。 频道下是否有商品。
		
		//如果有子频道，频道下有商品。 该频道不能删除。
		
		chdao.delete(channel);
		
		return true;
	}

	public boolean updateChannel(Channel channel) {
		
		this.chdao.update(channel);
		
		return true;
		
	}
	
	public List<Channel> getAllPrimaryChannel() {
		return chdao.getAllPrimaryChannel();
	}


	public List<Channel> getAllParentChannel() {
		
		return this.chdao.find("from Channel where (parentChannelId = '0' or parentChannelId is null) order by pri");
	}


	public boolean createChannel(Channel channel) {
		
		this.chdao.save(channel);
		
		return true;
	}


	public List<Channel> getAllPrimaryChannelByType(String type) {
		
		String args[]={type};

		return this.chdao.find("from Channel where (parentChannelId = '0' or parentChannelId is null) and type=? order by pri",args);
	}

	@SuppressWarnings("unchecked")
	public List<Channel> getAllPublicChannel() {
		String sql = "from Channel where status='01' and (parentChannelId = '0' or parentChannelId is null) order by pri";
		
		Query q = this.chdao.createQuery(sql);
		return q.list();
	}
}
